/*   
 * Copyright (c) 2010-2020 Founder Ltd. All Rights Reserved.   
 *   
 * This software is the confidential and proprietary information of   
 * Founder. You shall not disclose such Confidential Information   
 * and shall use it only in accordance with the terms of the agreements   
 * you entered into with Founder.   
 *   
 */
package com.wujw.auto.generate.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.wujw.auto.bean.ColumnStruct;
import com.wujw.auto.bean.TableStruct;
import com.wujw.auto.generate.GetTableDao;
import com.wujw.auto.parameter.CommonParameter;
import com.wujw.auto.utils.ConfigUtil;
import com.wujw.auto.utils.DataSourceUtil;

/**
 * 获取表信息
 * 
 * @author wujw
 * @version 1.0, 2017年11月10日
 */
public class GetTablesDaoImpl extends DataSourceUtil implements GetTableDao {

    @Override
    public Set<String> getTablesName() {
        Set<String> tables = new HashSet<>();
        if (!ConfigUtil.tablesList.isEmpty()) {
            tables = ConfigUtil.tablesList;
        } else {
            // 将生成所有表
            String sql = "show tables";

            ResultSet rs = this.query(sql);
            try {
                while (rs.next()) {
                    // 获得表名
                    tables.add(rs.getString(1));
                }
            } catch (Exception e) {
                // TODO: handle exception
            }
        }
        return tables;
    }

    @Override
    public List<TableStruct> getTablesStruct() {
        Set<String> tables = this.getTablesName();
        String sql = "";
        if(CommonParameter.JDBC_TYPE_MYSQL.equals(DataSourceUtil.getSqlType())) {
            sql = CommonParameter.MYSQL_TABLESTRCUT_SQL;
        }else if(CommonParameter.JDBC_TYPE_ORACLE.equals(DataSourceUtil.getSqlType())) {
            sql = CommonParameter.ORACLE_TABLESTRCUT_SQL;
        }else {
            System.out.println("生成失败------------ 不支持的数据库类型");
            System.exit(0);
        }
        String temp = sql;
        // 获取所有的表结构
        List<TableStruct> tabelStructList = new ArrayList<TableStruct>();
        for (String table : tables) {

            sql += table + "'";
            ResultSet rs = this.query(sql);
            // 取出信息
            List<ColumnStruct> columnStructList = new ArrayList<ColumnStruct>();
            try {
                while (rs.next()) {
                    ColumnStruct cs = new ColumnStruct();
                    cs.setColumnName(rs.getString(CommonParameter.COLUMN_NAME));
                    cs.setDateType(rs.getString(CommonParameter.COLUMN_TYPE));
                    cs.setColumnContent(rs.getString(CommonParameter.COLUMN_COMMENT));
                    columnStructList.add(cs);
                }
            } catch (SQLException e) {
                this.closeRes();
                e.printStackTrace();
            }
            // 放入表结构中
            TableStruct tableStruct = new TableStruct();
            tableStruct.setTableName(table);
            tableStruct.setColumns(columnStructList);
            tabelStructList.add(tableStruct);
            sql = temp;
            System.out.println(table + "  >>>>>>> 表信息组装完毕");
        }
        // 关闭连接
        this.closeRes();

        return tabelStructList;
    }

}
